package com.wwg.market.ui.service.interfaces;

import com.wwg.market.ui.service.dto.GeneralCodeDto;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2011-12-19
 * Time: 6:26:14
 * To change this template use File | Settings | File Templates.
 */
public class CodeServiceImpl implements CodeService {

    public List<GeneralCodeDto> getCodesByType(String code) {
        List result = new ArrayList();
        for (int i = 1; i <= 7; i++) {
            GeneralCodeDto dto = new GeneralCodeDto();
            dto.setName(code + "DM" + i);
            dto.setName(code + i);
            result.add(dto);
        }
        return result;
    }




    public List<GeneralCodeDto> getRootGeneralCodes() {
        List<GeneralCodeDto> list = new ArrayList<GeneralCodeDto>();
        for (int i = 0; i < 15; i++) {
            list.add(getMockCodeDto(i));
        }
        return list;
    }

    public List<GeneralCodeDto> getChildren(GeneralCodeDto parent) {
        List<GeneralCodeDto> list = new ArrayList<GeneralCodeDto>();
        for (long i = parent.getId() * 10; i < parent.getId() * 10 + 10; i++) {
            list.add(getMockCodeDto(i, parent));
        }
        return list;
    }

    public GeneralCodeDto saveOrUpdateCode(GeneralCodeDto codeDto) {
        System.out.println("saveOrUpdate codeDto = " + codeDto);
        if (codeDto.getId() == null) {
            GeneralCodeDto retDto = new GeneralCodeDto();
            retDto.setName(codeDto.getName());
            retDto.setCode(codeDto.getCode());
            retDto.setDescription(codeDto.getDescription());
            retDto.setId((long) (Math.random() * 100));
            retDto.setOrder(codeDto.getOrder());
            if (codeDto.getParent() != null) {
                retDto.setParent(codeDto.getParent());
            }
            return retDto;
        }
        return codeDto;
    }

    public void delete(GeneralCodeDto codeDto) {
        System.out.println("delete codeDto = " + codeDto);
    }

    public List<GeneralCodeDto> changeOrder(List<GeneralCodeDto> changeList) {
        return changeList;
    }

    public boolean checkCodeUnique(String code) {
        System.out.println("checkCodeUnique code = " + code);
        int i = (int) (Math.random() * 2);
        System.out.println("i = " + i);
        return i == 1;
    }


    private GeneralCodeDto getMockCodeDto(long i) {
        return getMockCodeDto(i, null);
    }

    private GeneralCodeDto getMockCodeDto(long i, GeneralCodeDto parent) {
        GeneralCodeDto codeDto = new GeneralCodeDto();
        codeDto.setCode(String.valueOf(i));
        codeDto.setDescription("描述" + i);
        codeDto.setId(i);
        codeDto.setName("代码" + i);
        codeDto.setOrder((int) i);
        if (parent != null) codeDto.setParent(parent.getId());
        return codeDto;
    }
}
